<?
require_once("class_GenericMultiComposit.php");
require_once("class_TableName.php");
require_once("class_FunctionName.php");

/********************************************************************************************************/
/* This class represents one row from any table which hase non composit not autoincremented PRIMARY KEY */
/********************************************************************************************************/

class SchemaName extends GenericMultiComposit {
    private $prod_sql;

    public function SchemaName($sn_id=false) {
	parent::__construct('schema_name','id',$sn_id);
	$this->add_composit('table_name','TableName','sn_id','id');
	$this->add_composit('function_name','FunctionName','sn_id','id');
    }

    public function set_prod_sql($prod_sql) {
	$this->prod_sql=$prod_sql;
    }

    public function update_schema($changes) {
	$old=$this->get_all_fields();
	if ($old['description'] != $changes['edit_sn__description']) {
	    $this->set_field('description',$changes['edit_sn__description']);
	}
	if ($old['observable'] != $changes['edit_sn__observable']) {
	    $this->set_field('observable',$changes['edit_sn__observable']);
	}
	$this->save();
    }


    public function discover_tables() {
/*
 * For cron based jobs
 */
	$tbl_stat_dicover_condition=array('alive'=>'t');
	$this->initiate_composit('table_name',$tbl_stat_dicover_condition);

	$exist_ids=$this->get_composit('table_name',COLLECTION_ID_ARRAY);

	$t_d_q="SELECT r.oid,r.relname,r.reltoastrelid,
    CASE WHEN h.inhrelid IS NULL THEN 'f'::boolean ELSE 't'::boolean END AS has_parent
    FROM pg_class r
    LEFT JOIN pg_inherits h ON r.oid=h.inhrelid
    WHERE r.relkind='r'
    AND r.relnamespace=".$this->get_field('obj_oid');

	$this->prod_sql->select_c($t_d_q);
	foreach ($this->prod_sql->get_result() as $prod_tbl) {
	    $exist_id=$this->composit_obj_exists('table_name','obj_oid',$prod_tbl['oid']);
	    if (!$exist_id) {

		$new_tn=new TableName;
		$new_tn->set_field('sn_id',$this->id);
		$new_tn->set_field('obj_oid',$prod_tbl['oid']);
		$new_tn->set_field('has_parent',$prod_tbl['has_parent']);
		$new_tn->set_field('tbl_name',$prod_tbl['relname']);
		$new_tn->save();
	
		$this->add_composit_object('table_name',$new_tn->get_id());
		if ($prod_tbl['reltoastrelid'] != 0) {
		    $new_tn->add_toast($prod_tbl['reltoastrelid'],$this->prod_sql);
		    $new_tn->discover_indexes($this->prod_sql);
		}

	    } else {
		foreach ($exist_ids as $id=>$val) {
		    if ($val == $exist_id) {
			$exist_tn=new TableName($exist_id);
			$exist_tn->discover_toast($this->prod_sql);
			$exist_tn->discover_indexes($this->prod_sql);
			unset($exist_ids[$id]);
		    }
		}
	    }
	}
	foreach ($exist_ids as $old_id) {
	    $old_tn=new TableName($old_id);
	    $old_tn->set_field('alive','f');
	    $old_tn->save();
	    $this->remove_composit_object('table_name',$old_id);
	}
    }


    public function discover_functions() {
/*
 * For cron based jobs
 */
	$pro_stat_dicover_condition=array('alive'=>'t');
	$this->initiate_composit('function_name',$pro_stat_dicover_condition);

	$exist_ids=$this->get_composit('function_name',COLLECTION_ID_ARRAY);

	$this->prod_sql->select_c("SELECT p.oid,p.proname AS funcname,p.proretset,t.typname,l.lanname
	FROM pg_proc p
	LEFT JOIN pg_namespace n ON n.oid = p.pronamespace
	JOIN pg_type t ON p.prorettype=t.oid
	JOIN pg_language l ON p.prolang=l.oid
	WHERE (p.prolang <> (12)::oid)
	AND n.oid=".$this->get_field('obj_oid'));
	
	foreach ($this->prod_sql->get_result() as $prod_func) {
	    $exist_id=$this->composit_obj_exists('function_name','pro_oid',$prod_func['oid']);
	    if(!$exist_id) {
		$new_fn=new FunctionName;
		$new_fn->set_field('sn_id',$this->id);
		$new_fn->set_field('pro_oid',$prod_func['oid']);
		$new_fn->set_field('proretset',$prod_func['proretset']);
		$new_fn->set_field('func_name',$prod_func['funcname']);
		$new_fn->set_field('prorettype',$prod_func['typname']);
		$new_fn->set_field('prolang',$prod_func['lanname']);
		$new_fn->save();
		$this->add_composit_object('function_name',$new_fn->get_id());
	    } else {
		foreach ($exist_ids as $id=>$val) {
		    if ($val==$exist_id) {
			unset($exist_ids[$id]);
		    }
		}
	    }
	}
	foreach ($exist_ids as $old_id) {
	    $old_fn=new FunctionName($old_id);
	    $old_fn->set_field('alive','f');
	    $old_fn->save();
	    $this->remove_composit_object('function_name',$old_id);
	}
    }

    public function populate_table_stat($time_id) {
	foreach ($this->get_composit('table_name',COLLECTION_OBJECT_ARRAY) as $tn) {
	    $tn->get_stat($time_id,$this->prod_sql);
	}
    }

    public function populate_function_stat($time_id) {
	foreach ($this->get_composit('function_name',COLLECTION_OBJECT_ARRAY) as $fn) {
	    $fn->get_stat($time_id,$this->prod_sql);
	}
    }







    public function print_config_table_row($added) {
	if (!$this->loaded) {
	    $this->load();
	}
	$rowspan=$this->composit_array['table_name']['collection']->get_item_count()+1;
	if (in_array($this->database_fields['sch_name'],$added)) {
	    $string= "<tr valign=top bgcolor=#DDD>";
	} else {
	    $string="<tr valign=top>";
	}
	
	$string.="<td rowspan=".$rowspan." bgcolor=#CCC><b>".$this->database_fields['sch_name']."</b></td>
	    <td rowspan=".$rowspan.">".$this->database_fields['description']."</td>
	    <td rowspan=".$rowspan." align=center>".($this->database_fields['observable'] == 't' ? 'Yes' : '<i>No</i>')."</td>
	    <td rowspan=".$rowspan."><a href=".$_SERVER['PHP_SELF']."?action=edit&content=schema_name&sn_id=".$this->id."><i>Edit</i></a></td>
	    <td>&nbsp;</td>";
	$string.="</tr>";
	foreach ($this->composit_array['table_name']['collection']->get_object_array() as $tn) {
	    $string.="<tr><td bgcolor=#EEE valign=top>".$tn."</td><td><table width=100% border=0>";
	    foreach ($tn->get_composit_array() as $in) {
		$string.=$in->print_config_table_row();
	    }
	    $string.="</table></tr>";
	}
	$string.="<tr><td colspan=6><hr width=80%></td></tr>";
	return $string;
    }

    public function __toString() {
	return $this->get_field('sch_name');
    }
}

?>
